/*
 * Copyright 2020-2030, MateCloud, DAOTIANDI Technology Inc All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * Author: pangu(7333791@qq.com)
 */
package com.yc.cloud.oss.controller;

import cn.hutool.core.util.StrUtil;
import com.yc.cloud.basic.api.ResponseResult;
import com.yc.cloud.oss.dto.request.OssUploadBase64Param;
import com.yc.cloud.oss.dto.request.OssUploadParam;
import com.yc.cloud.oss.service.OssFileService;
import com.yc.cloud.oss.vo.OssFileVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.val;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.security.Principal;

/**
 * <p>
 * 文件 前端控制器
 * </p>
 *
 * @author Yancy
 * @since 2023-12-25
 */
@RestController
@RequestMapping("/oss/files")
@Api(value = "文件", tags = "文件接口")
public class OssFileController {

    @Resource
    private OssFileService ossFileService;

    @PostMapping
    @ApiOperation(value = "上传文件", notes = "添加")
    public ResponseResult<OssFileVo> upload(@RequestPart("file") MultipartFile file, @ApiIgnore Principal principal,@Validated OssUploadParam param) {
        //使用用户名作为存储桶名字
        val name = principal.getName();
       // val param = new OssUploadParam();
       if(StrUtil.isEmpty(param.getBucket())){
          param.setBucket(name);
       }
       if(StrUtil.isEmpty(param.getFolder())){
          param.setFolder("");
       }
       if(param.getRename()==null){
         param.setRename(false);
       }
        val result = ossFileService.upload(file, param);
        return ResponseResult.success(result);
    }


    @PostMapping("upload/base64")
    @ApiOperation(value = "base64方式上传文件", notes = "添加")
    public ResponseResult<OssFileVo> uploadByBase64(@ApiIgnore Principal principal, @RequestBody OssUploadBase64Param param) {
        //使用用户名作为存储桶名字
        val name = principal.getName();
        param.setBucket(name);
        val result = ossFileService.uploadByBase64(param);
        return ResponseResult.success(result);
    }

//    @GetMapping
//    @ApiOperation(value = "文件列表", notes = "支持分页查询,字段模糊匹配,排序等")
//    public ResponseResult<CommonPage<OssFile>> list(BaseQueryParam param, @ApiIgnore Principal principal) {
////        principal.getName()
//        val result = ossFileService.list(param);
//        return ResponseResult.success(CommonPage.restPage(result));
//    }


    @GetMapping("{fileid}")
    @ApiOperation(value = "文件详情", notes = "根据文件标识查询")
    public ResponseResult<OssFileVo> getByFileId(@PathVariable @ApiParam(value = "fileid", required = true) String fileid) {
        return ResponseResult.success(ossFileService.getByFileId(fileid));
    }


    @ApiOperation(value = "删除文件", notes = "通过文件标识删除")
    @DeleteMapping(value = "{fileid}")
    public ResponseResult<Boolean> deleteByFileId(@PathVariable @ApiParam(value = "fileid", required = true) String fileid) {
        val flag = ossFileService.deleteByFileId(fileid);
        if (flag) {
            return ResponseResult.success(true);
        }
        return ResponseResult.failed();
    }


//    @ApiOperation(value = "修改文件", notes = "通过ID修改")
//    @ApiImplicitParams({@ApiImplicitParam(name = "id", required = true, value = "ID", paramType = "path", dataTypeClass = Long.class),})
//    @PutMapping(value = "{id}")
//    public ResponseResult<Long> update(@PathVariable Long id, @Validated @RequestBody OssFile ossFile) {
//        ossFile.setId(id);
//        val result = ossFileService.updateById(ossFile);
//        if (result) {
//            return ResponseResult.success();
//        }
//        return ResponseResult.failed();
//    }
}
